package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.logistics.dao.mapper.IVehicleCardMapper;
import com.xhwl.logistics.entity.VehicleCard;
import com.xhwl.logistics.enums.VehicleCardTypeEnum;
import com.xhwl.logistics.vo.VehicleCardTypeVO;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

//@Service
public class VehicleCardServiceImpl extends ServiceImpl<IVehicleCardMapper, VehicleCard> {

    public Wrapper<VehicleCard> conditionForQueryCards(List<Integer> projectIdList, Integer cardType) {
        LambdaQueryWrapper<VehicleCard> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .in(VehicleCard::getProjectId, projectIdList)
                .eq(Objects.nonNull(cardType), VehicleCard::getCardType, cardType);
    }

    public Wrapper<VehicleCard> conditionForQueryCards(Integer projectId) {
        LambdaQueryWrapper<VehicleCard> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .eq(VehicleCard::getProjectId, projectId);
    }

    private Wrapper<VehicleCard> conditionForDeleteCards(List<Integer> idList) {
        LambdaUpdateWrapper<VehicleCard> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .in(VehicleCard::getId, idList)
                .set(VehicleCard::getDeleted, 1)
                .set(VehicleCard::getDeleterAccountName, "xxl-job")
                .set(VehicleCard::getUpdatedAt, new Date());
    }

    public void deleteCards(List<Integer> idList) {
        update(conditionForDeleteCards(idList));
    }


    /**
     * 根据项目id查询该项目上有的卡类型
     */
    public List<VehicleCardTypeVO> listCardTypeByProjectId(Integer projectId){
        List<VehicleCard> vehicleCardList = this.list(new LambdaQueryWrapper<VehicleCard>().eq(VehicleCard::getProjectId, projectId));
        List<Integer> cardTypeList = vehicleCardList.stream().map(VehicleCard::getCardType).distinct().collect(Collectors.toList());
        return cardTypeList.stream()
                .map(cardType -> new VehicleCardTypeVO(VehicleCardTypeEnum.toVehicleCardTypeEnum(cardType).getDescription()))
                .collect(Collectors.toList());
    }
}
